package com.five.reader.controller;

import com.five.exception.BusinessException;
import com.five.pojo.ReadHistory;
import com.five.reader.service.ReadHistoryService;
import com.five.result.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/read-history")
@RequiredArgsConstructor
public class ReadHistoryController {

    private final ReadHistoryService readHistoryService;

    /**
     * 添加阅读记录
     */
    @PostMapping("/add")
    public Result<Boolean> addReadHistory(@RequestBody ReadHistory readHistory) {
        try {
            boolean result = readHistoryService.addReadHistory(readHistory);
            return Result.success(result ? "添加成功" : "添加失败");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 获取读者的阅读记录
     */
    @PostMapping("/list-by-reader")
    public Result<List<ReadHistory>> getReadHistoryByReader(@RequestBody Map<String, Long> request) {
        try {
            Long readerId = request.get("readerId");
            if (readerId == null) {
                return Result.error("读者ID不能为空");
            }
            List<ReadHistory> historyList = readHistoryService.getReadHistoryByReader(readerId);
            return Result.success(historyList);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 获取读者最近阅读记录
     */
    @PostMapping("/recent")
    public Result<List<ReadHistory>> getRecentReadHistory(@RequestBody Map<String, Object> request) {
        try {
            Long readerId = request.get("readerId") != null ? 
                Long.valueOf(request.get("readerId").toString()) : null;
            Integer limit = request.get("limit") != null ? 
                Integer.valueOf(request.get("limit").toString()) : 10;
            
            if (readerId == null) {
                return Result.error("读者ID不能为空");
            }
            List<ReadHistory> historyList = readHistoryService.getRecentReadHistory(readerId, limit);
            return Result.success(historyList);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 获取读者对某本小说的阅读记录
     */
    @PostMapping("/list-by-novel")
    public Result<List<ReadHistory>> getReadHistoryByReaderAndNovel(@RequestBody Map<String, Long> request) {
        try {
            Long readerId = request.get("readerId");
            Long novelId = request.get("novelId");
            
            if (readerId == null) {
                return Result.error("读者ID不能为空");
            }
            if (novelId == null) {
                return Result.error("小说ID不能为空");
            }
            List<ReadHistory> historyList = readHistoryService.getReadHistoryByReaderAndNovel(readerId, novelId);
            return Result.success(historyList);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 删除阅读记录
     */
    @PostMapping("/delete")
    public Result<Boolean> deleteReadHistory(@RequestBody Map<String, Long> request) {
        try {
            Long id = request.get("id");
            if (id == null) {
                return Result.error("阅读记录ID不能为空");
            }
            boolean result = readHistoryService.deleteReadHistory(id);
            return Result.success(result ? "删除成功" : "删除失败");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 统计读者总阅读时长
     */
    @PostMapping("/total-read-time")
    public Result<Integer> getTotalReadTime(@RequestBody Map<String, Long> request) {
        try {
            Long readerId = request.get("readerId");
            if (readerId == null) {
                return Result.error("读者ID不能为空");
            }
            Integer totalTime = readHistoryService.getTotalReadTime(readerId);
            return Result.success(totalTime);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }

    /**
     * 统计小说被阅读次数
     */
    @PostMapping("/novel-read-count")
    public Result<Integer> getNovelReadCount(@RequestBody Map<String, Long> request) {
        try {
            Long novelId = request.get("novelId");
            if (novelId == null) {
                return Result.error("小说ID不能为空");
            }
            Integer readCount = readHistoryService.getNovelReadCount(novelId);
            return Result.success(readCount);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error("系统错误: " + e.getMessage());
        }
    }
}